package com.yhjapp.onethingoneyard.onethingoneyard;

import android.app.Activity;
import android.app.Presentation;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.display.DisplayManager;
import android.media.MediaPlayer;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.utils.L;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;

import java.io.IOException;

/**
 * Created by Administrator on 2018/1/4.
 */

public class RankingRanNo1HtmlActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, SurfaceHolder.Callback{


    private static final String TAG = "Bradlley";
    private DifferentDislay mPresentation;
    private MediaPlayer mMediaPlayer;
    private SurfaceView mSurfaceView;
    boolean close=true;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_jd_html);


        LoadClose();
        DisplayManager manager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = manager.getDisplays();
        // displays[0] 主屏
        // displays[1] 副屏
        if (mPresentation == null) {
            DifferentDislay differentDislay = new DifferentDislay(this, displays[1]);
            differentDislay.getWindow().setType(
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            differentDislay.show();
        }

//        url = getIntent().getStringExtra("url");
//        title = getIntent().getStringExtra("title");
//
//        if(title.equals("d")){
//            ll_html.setVisibility(View.GONE);
//            LoadClose();
//        }else {
//            ll_html.setVisibility(View.VISIBLE);
//            tv_html_tilte.setText(title + "");
//        }


//    if(userid.equals("")) {
//
//    }else{
//
//    }
//        Log.e("TAF", url );

    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated: ");
        try {
            mMediaPlayer.setDataSource("/sdcard/Movies/mv.wmv");
            mMediaPlayer.setDisplay(mSurfaceView.getHolder());
            mMediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        Log.d(TAG, "onPrepared: ");
        mMediaPlayer.start();
    }

    private void LoadClose() {
        String url="http://develop.jhjvip.cn:8080/mall/mobile/ywym/isClose";
        OkHttpUtils.get().url(url).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Gson gson=new Gson();
                CloseBean list=gson.fromJson(response, CloseBean.class);
                if(list.getCode().equals("10000")){
                    if(list.getData().equals("close")){
                        close=false;
                    }
                }

            }
        });

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        // TODO Auto-generated method stub

        if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
            if(close){
                finish();
                System.exit(0);
            }else{
                System.out.println("返回键执行了！！！");
            }

            return true;
        }
//        if (keyCode == event. KEYCODE_HOME) {
//            if(close){
//
//            }else{
//                System.out.println("返回键执行了！！！");
//            }
//            return true;
//        }

          return super.onKeyDown(keyCode, event);

    }



}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
class DifferentDislay extends Presentation {
    private WebView ll_web_container;
    private String url;
    private ImageView iv_html_back;
    private TextView tv_html_tilte;
    private String title;
    private LinearLayout ll_html;
    Context outerContext;

    public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;
    private String userid="";
    public DifferentDislay(Context outerContext, Display display) {
        super(outerContext,display);
        this.outerContext=outerContext;

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_screen);
        ll_web_container = (WebView) findViewById(R.id.ll_container);
        iv_html_back= (ImageView) findViewById(R.id.iv_html_back);
        ll_html= (LinearLayout) findViewById(R.id.ll_html);
        iv_html_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity activity=new Activity();
                activity.finish();
            }
        });
        tv_html_tilte= (TextView) findViewById(R.id.tv_html_tilte);
        Log.v("shiran","-------------WEBACTIVITY-----------------");
        Log.e("phone",SPUtils.getInstance().getString(SPConstants.PHONE)+"    "+SPUtils.getInstance().getString(SPConstants.PWD));
        if(SPUtils.getInstance().getString(SPConstants.PHONE)!=""||SPUtils.getInstance().getString(SPConstants.PWD)!=""){

            LoadLogin();

            ll_html.setVisibility(View.GONE);
//            LoadClose();
        }else{
            Intent intent=new Intent(outerContext,LoginActivity.class);
            outerContext.startActivity(intent);
            Activity activity=new Activity();
            activity.finish();

        }
    }
    private void LoadLogin(){


        String url = "http://develop.jhjvip.cn:8080/mall/mobile/login/in"+"?phone="+SPUtils.getInstance().getString(SPConstants.PHONE)+"&pwd="+SPUtils.getInstance().getString(SPConstants.PWD)+"&appId="+ AUniqueIdentifier.getDeviceId(outerContext);
        Log.e("","登录url===="+url);
        OkHttpUtils.get().url(url).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                LoginBean list = gson.fromJson(response, LoginBean.class);
                if(list.getCode().equals("10000")) {
//                    if (list != null && !TextUtils.isEmpty(list.getData().getToken())) {
//                        SPUtils.getInstance().put(SPConstants.KEY_USER_TOKEN, list.getData().getToken());
//                        SPUtils.getInstance().put(SPConstants.KEY_ISSTORE,list.getIsStore());
//                        SPUtils.getInstance().put(SPConstants.KEY_USERID,list.getData().getUserid());
//                    }
                    userid = list.getData().getUserid();
                    Toast.makeText(outerContext, "登录成功", Toast.LENGTH_SHORT).show();
//                    Log.e("",""+SPUtils.getInstance().getString(SPConstants.KEY_ISSTORE));
                    String url= "http://47.92.134.181:8080/mall/mobile/ywym/index?userid="+userid;
                    ll_web_container.loadUrl(url);
                    ll_web_container.getSettings().setJavaScriptEnabled(true);
                    WebviewSettingsinit();
                }else {
//                    SPUtils.getInstance().put(SPConstants.KEY_ISSTORE,list.getIsStore());
                    Toast.makeText(outerContext, list.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
    private void WebviewSettingsinit() {
        ll_web_container.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, url);

            }
        });
        ll_web_container.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });

    }
}
